local skel = fk.CreateSkill {
  name = "emo__jiqi",
}

Fk:loadTranslationTable{
  ["emo__jiqi"] = "集奇",
  [":emo__jiqi"] = "你失去了【杀】/【闪】的阶段结束时，你可以摸一张锦囊牌/装备牌，然后你可以使用这些牌，结算后可交给其他角色。",

  ["#emo__jiqi-use"] = "集奇：你可以使用这些牌",
  ["#emo__jiqi-give"] = "集奇：你可以将 %arg 交给一名其他角色",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local list = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from and move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                local card = Fk:getCardById(info.cardId)
                if table.contains({"slash", "jink"}, card.trueName) then
                  table.insertIfNeed(list, card.trueName == "slash" and "trick" or "equip")
                end
              end
            end
          end
        end
        return #list == 2
      end, Player.HistoryPhase)
      if #list > 0 then
        event:setCostData(self, list)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.dead then return end
    local list = event:getCostData(self)
    local ids = {}
    for _, name in ipairs(list) do
      local c = room:getCardsFromPileByRule(".|.|.|.|.|"..name)
      if #c > 0 then
        table.insert(ids, c[1])
      end
    end
    if #ids == 0 then return false end
    room:obtainCard(player, ids, false, fk.ReasonPrey, player, skel.name)
    while player:isAlive() and #ids > 0 do
      ids = table.filter(ids, function (id) return table.contains(player.player_cards[Player.Hand], id) end)
      if #ids == 0 then break end
      local use = room:askToUseRealCard(player, {
        pattern = ids, skill_name = skel.name, skip = true, prompt = "#emo__jiqi-use",
      })
      if not use then break end
      local cid = use.card.id
      table.removeOne(ids, cid)
      room:useCard(use)
      if player.dead then break end
      if table.contains(player:getCardIds("hej"), cid) or room:getCardArea(cid) == Card.DiscardPile then
        local to = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player), min_num = 1, max_num = 1, skill_name = skel.name,
          prompt = "#emo__jiqi-give:::" .. use.card:toLogString(),
        })[1]
        if to then
          room:obtainCard(to, {cid}, true, fk.ReasonGive, player, skel.name)
        end
      end
    end
  end,
})

return skel
